package com.demo.controller_admin;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.websocket.server.PathParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.WebRequest;
import com.demo.entity.User;
import com.demo.service_admin.UserService;

@RestController("adminUserController")
@RequestMapping(value = "/${site.adminUrlPrefix}/user")
public class UserController {
	
	@Resource(name = "adminUserService")
	UserService userService;

	@PostMapping(value = "/save")
	public User save(User user) {
		return userService.save(user);
	}

	@DeleteMapping(value = "/delete/{id}")
	public void delete(@PathVariable("id") Long id) {
		userService.delete(id);
	}

	@PutMapping(value = "/edit/{id}")
	public User update(@PathVariable("id") Long id, User user) {
		return userService.save(user);
	}

	@GetMapping(value = "/list")
	public List<User> findAll() {
		return userService.findAll();
	}

	@GetMapping(value = "/{id}")
	public User findById(@PathVariable("id") Long id) {
		return userService.findById(id);
	}

	@GetMapping(value = "/username/{username}")
	public User findByUsername(@PathVariable("username") String username) {
		return userService.findByUsername(username);
	}

	/**
	 * 查找日期在一个区间范围内的数据
	 * 
	 * @param birth
	 * @param birth2
	 * @return
	 */
	@GetMapping(value = "/birth_between")
	public List<User> findByBirthBetween(@PathParam("birth") Date birth, @PathParam("birth2") Date birth2) {
		return userService.findByBirthBetween(birth, birth2);
	}

	@InitBinder
	public void initBinder(WebDataBinder binder, WebRequest request) {
		DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
	}

	/**
	 * 根据用户名模糊查询并分页显示
	 * 
	 * @param username
	 * @param page
	 * @return
	 */
	@GetMapping(value = "/q/{username}")
	public Page<User> findByUsernameLike(@RequestParam(defaultValue="1",value="p") Integer page, @PathVariable("username") String username) {
		int pageSize = 2;
		page = page > 0 ? page - 1 : page;
		//log.warn("page=" + page);
		Pageable pageable = PageRequest.of(page, pageSize, Direction.DESC, "id");
		return userService.findByUsernameLike(username, pageable);
		//分页查询的逻辑
		//1 count 查询符合条件的总记录数，根据设定的每页大小计算总页数
		//2 select * from 表 limit 偏移量,每页大小
	}

	/**
	 * 复杂动态sql查询，如 SELECT count(1) FROM t_user u WHERE 1 = 1 AND u.sex = ? AND
	 * u.height = ? AND u.username like concat('%',?,'%')
	 * 
	 * @param page
	 * @param user
	 * @return
	 */
//	@GetMapping(value = "/search")
//	public Page<User> findUserByDynamicSql(@RequestParam(defaultValue="1",value="p") Integer page, User user) {
//		int pageSize = 3;//每页大小
//		page = page > 0 ? page - 1 : page;
//		Pageable pageable = PageRequest.of(page, pageSize);//构建Pageable对象，设置每页大小，获取当前页码
//		//log.info(user.toString());
//		return userService.search(pageable, user);
//	}
}